/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.utils.ConvertToHtml;
import com.neusoft.lohas.common.utils.HTMLSpirit;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.merchant.ActivityPoster;
import com.neusoft.lohas.modules.ivm.service.merchant.ActivityPosterService;
import com.neusoft.lohas.modules.sys.entity.User;
import com.neusoft.lohas.modules.sys.utils.UserUtils;

/**
 * 小店公告Controller
 * @author Jackie.Liu
 * @version 2014-10-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/activity/poster")
public class ActivityPosterController extends BaseController {

	@Autowired
	private ActivityPosterService activityPosterService;
	
	@ModelAttribute
	public ActivityPoster get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return activityPosterService.get(id);
		}else{
			return new ActivityPoster();
		}
	}
	
	@RequiresPermissions("ivm:activity:poster:view")
	@RequestMapping(value = {"list", ""})
	public String list(ActivityPoster activityPoster, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			activityPoster.setCreateBy(user);
		}
        Page<ActivityPoster> page = activityPosterService.find(new Page<ActivityPoster>(request, response), activityPoster); 
        model.addAttribute("page", page);
		return "modules/ivm/set/activityPosterList";
	}

	@RequiresPermissions("ivm:activity:poster:view")
	@RequestMapping(value = "form")
	public String form(ActivityPoster activityPoster, Model model) {
		if (StringUtils.isBlank(activityPoster.getId())){
			activityPoster.setMerchant(super.merchant);
			activityPoster = activityPosterService.findByMerchantId(activityPoster.getMerchant().getId());
		}
		model.addAttribute("activityPoster", activityPoster);
		return "modules/ivm/set/activityNoticeForm";
	}

	@RequiresPermissions("ivm:activity:poster:edit")
	@RequestMapping(value = "save")
	public String save(ActivityPoster activityPoster, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, activityPoster)){
			return form(activityPoster, model);
		}
		activityPoster.setContent(ConvertToHtml.convertor(
				activityPoster.getContent()));
		if (StringUtils.isNotEmpty(activityPoster.getContent())) {
			String brief = HTMLSpirit.delHTMLTag(ConvertToHtml
					.convertor(activityPoster.getContent()));
			brief = brief.replaceAll(" ", "");
			brief = brief.length()>30?brief.substring(0, 30)+"...":brief;
			activityPoster.setBrief(brief);
		}
		activityPosterService.save(activityPoster);
		addMessage(redirectAttributes, "保存小店公告成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/activity/poster/form";
	}
	
	@RequiresPermissions("ivm:activityPoster:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		activityPosterService.delete(id);
		addMessage(redirectAttributes, "删除小店公告成功");
		return "redirect:"+Global.getAdminPath()+"/modules/ivm/activity/poster/?repage";
	}

}
